package com.lankystar.sos.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lankystar.sos.dao.ILifeCountDao;
import com.lankystar.util.MathMethod;

@Repository
public class LifeCountDaoImpl implements ILifeCountDao{

	@Autowired
	private SessionFactory sf;
	public Session getSession(){
		System.err.println("session开启！！");
		return sf.getCurrentSession();
	}
	@Override
	public Map<String, Object> selectTypeCount(Double longitude, Double latitude) {
		Map<String, Object> map = MathMethod.distance(longitude, latitude);
		String sql_hosp = "select count(*) from Hospital h where h.longitude between "+map.get("minlng")+" and "
					+map.get("maxlng")+" and h.latitude between "+map.get("minlat")+" and "+map.get("maxlat")+"";
		long count_hosp =  (long) getSession().createQuery(sql_hosp).uniqueResult();
		String sql_phar = "select count(*) from Pharmacy p where p.longitude between "+map.get("minlng")+" and "
					+map.get("maxlng")+"  and p.latitude between "+map.get("minlat")+" and "+map.get("maxlat")+"";
		long count_phar = (long) getSession().createQuery(sql_phar).uniqueResult();
		String sql_user = "select count(*) from UserLocation ul where ul.longitude between "+map.get("minlng")+" and "
					+map.get("maxlng")+"  and ul.latitude between "+map.get("minlat")+" and "+map.get("maxlat")+"";
		long count_user = (long) getSession().createQuery(sql_user).uniqueResult();
		Map<String, Object> maps = new HashMap<>();
		maps.put("Hospitalcount", count_hosp);
		maps.put("Pharmacycount", count_phar);
		maps.put("Usercount", count_user);
		return maps;
	}

}
